#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
 
class Solution
{
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root)
    {
        vector<vector<int>> ret;
        queue<TreeNode*> q;
        if (root == nullptr) return ret;
        q.push(root);
        bool level = true;
        while (q.size())
        {
            int sz = q.size();
            vector<int> path;
            for (int i = 0; i < sz; i++)
            {
                TreeNode* t = q.front();
                q.pop();
                path.push_back(t->val);
                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);

            }
            if (!level) reverse(path.begin(), path.end());
            ret.push_back(path);
            level = !level;
        }
        return ret;
    }
};